home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 466_01 / SRC / FMTSPEC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-20  |  4.8 KB  |  277 lines

  1. #include <afx.h>
  2. #include <afxtempl.h>
  3. #include "parse.h"
  4. #include "topiclog.h"
  5. #include "fmtspec.h"
  6. #include "errmsg.h"
  7.  
  8. TAGSPEC SECTIONS[] =
  9. {
  10.     sectFILE,       "file",
  11.     sectTOPIC,      "topic",
  12.     sectPARAGRAPH,  "paragraph",
  13.     sectTYPE,       "text",
  14.     sectDIAGRAM,    "diagram",
  15.     sectCONSTANT,   "constant",
  16.     sectINDEX,      "index",
  17.     sectEXTENSION,    "extension",
  18.     sectToken,        "token",
  19.     -1,             NULL,
  20. };
  21.  
  22.  
  23. int SeekSection(CFmtInput &in)
  24. {
  25.     int nRet;
  26.     const char *sz;
  27.  
  28.     do
  29.     {
  30.         nRet = in.GetLine();
  31.         if(nRet)
  32.             return nRet;
  33.  
  34.         if(in.m_szLineBuf[0] == chFmtSectOpen)
  35.             return 0;
  36.  
  37.         sz = EatWhite(in.m_szLineBuf);
  38.  
  39.         switch(*sz)
  40.         {
  41.             // blank or comment line
  42.             case '\0':
  43.             case '\r': 
  44.             case '\n':
  45.             case chFmtComment:
  46.                 break;
  47.                 
  48.             default:
  49.                 return fmterrExpectedEol;
  50.         }
  51.  
  52.     } while( !in.EndOfFile() );
  53.  
  54.     return 0;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. int GetSection(char *szSrc, int *piSection)
  61. {
  62.     const char *szCont = SeekEnd(szSrc+1, chCloseBracket, MAXTAGSIZE);
  63.     
  64.     if(szCont == NULL || *szCont != chFmtSectClose)
  65.         return FALSE;
  66.  
  67.     int i;
  68.  
  69.     CString sTag(szSrc+1, szCont-(szSrc+1));
  70.     
  71.     for(i = 0; SECTIONS[i].iTag != -1; i++)
  72.     {
  73.         if(_stricmp(sTag, SECTIONS[i].szName) == 0)
  74.         {
  75.             *piSection = SECTIONS[i].iTag;
  76.             return TRUE;
  77.         }
  78.     }
  79.     return FALSE;
  80. }
  81.  
  82.  
  83.  
  84. CFmtList *GetFmtList(
  85.     FormatInfo &fmts,
  86.     int nSection)
  87. {
  88.     switch(nSection)
  89.     {
  90.         case sectFILE:
  91.             return &fmts.file;
  92.                                 
  93.         case sectTOPIC:
  94.             return &fmts.topic;
  95.             
  96.         case sectPARAGRAPH:
  97.             return &fmts.paragraph;
  98.             
  99.         case sectTYPE:
  100.             return &fmts.text;
  101.             
  102.         case sectDIAGRAM:
  103.             return &fmts.diagram;
  104.             
  105.         case sectCONSTANT:
  106.             return &fmts.constant;
  107.  
  108.         case sectEXTENSION:
  109.             return &fmts.extension;
  110.  
  111.         case sectINDEX:
  112.             return &fmts.index;
  113.  
  114.         case sectToken:
  115.             return &fmts.token;
  116.     }
  117.  
  118.     ASSERT(0);
  119.  
  120.     return NULL;
  121. }
  122.  
  123.  
  124. int GetFormatSection(
  125.     CFmtInput &in, 
  126.     FormatInfo &fmts)
  127. {
  128.     int  nRet;
  129.     int  nSection;
  130.     CFmtList    *pfmt;
  131.  
  132.     // Find the opening of the section.
  133.     
  134.     nRet = SeekSection(in);
  135.     
  136.     // Exit on error or EOF.
  137.  
  138.     if(nRet)
  139.         return nRet;
  140.  
  141.     if(in.EndOfFile())
  142.         return 0;
  143.  
  144.     // Get the section identifier.
  145.     
  146.     if(!GetSection(in.m_szLineBuf, &nSection))
  147.         return fmterrBadSection;
  148.     
  149.     pfmt = GetFmtList(fmts, nSection);
  150.     
  151.     nRet = pfmt->GetSection(in);
  152.     if(nRet)
  153.         return nRet;
  154.  
  155.     return 0;
  156. }
  157.  
  158.  
  159.     
  160.  
  161. int GetFormatFile(
  162.     const char *szFile,
  163.     const char *szAutoduckPath,
  164.     const char *szOutputType,
  165.     FormatInfo &fmt,
  166.     BOOL bVerbose)
  167. {
  168.     CFmtInput file;
  169.     int nRet;
  170.  
  171.     int nFile = fmt.asSrcFn.GetSize();
  172.     fmt.asSrcFn.SetSize(nFile+1);
  173.     fmt.asSrcFn[nFile] = new CString(szFile);
  174.  
  175.     file.SetOutputType(szOutputType);
  176.     file.m_nFile = nFile;
  177.  
  178.     fmt.lErrLine = NO_LINE;
  179.     
  180.     nRet = file.OpenFmtFile(szFile, szAutoduckPath);
  181.     if(nRet)
  182.         return nRet;
  183.     
  184.     if(bVerbose)
  185.         fprintf(stdout, "Getting tags from %s\n", (const char *)file.m_szFilename);
  186.     
  187.     do {
  188.         
  189.         nRet = GetFormatSection(file, fmt);
  190.         if(nRet)
  191.         {
  192.             fmt.lErrLine = file.m_lCurLine;
  193.             return nRet;
  194.         }
  195.  
  196.     } while( ! file.EndOfFile() );
  197.  
  198.     file.Close();
  199.  
  200.     return 0;
  201. }
  202.  
  203.  
  204.  
  205. BOOL ValidateFormats(
  206.     FormatInfo &fmts)
  207. {
  208.     int nRet;
  209.     BOOL bSuccess = TRUE;
  210.  
  211.     nRet = fmts.file.Validate();
  212.     if(nRet) 
  213.     {
  214.         PrintError(NULL, NO_LINE, nRet);
  215.         bSuccess = FALSE;
  216.     }
  217.  
  218.     nRet = fmts.topic.Validate(); 
  219.     if(nRet) 
  220.     {
  221.         PrintError(NULL, NO_LINE, nRet);
  222.         bSuccess = FALSE;
  223.     }
  224.  
  225.     nRet = fmts.paragraph.Validate(); 
  226.     if(nRet) 
  227.     {
  228.         PrintError(NULL, NO_LINE, nRet);
  229.         bSuccess = FALSE;
  230.     }
  231.  
  232.     nRet = fmts.text.Validate(); 
  233.     if(nRet) 
  234.     {
  235.         PrintError(NULL, NO_LINE, nRet);
  236.         bSuccess = FALSE;
  237.     }
  238.  
  239.     nRet = fmts.diagram.Validate(); 
  240.     if(nRet) 
  241.     {
  242.         PrintError(NULL, NO_LINE, nRet);
  243.         bSuccess = FALSE;
  244.     }
  245.  
  246.     nRet = fmts.constant.Validate(); 
  247.     if(nRet)
  248.     {
  249.         PrintError(NULL, NO_LINE, nRet);
  250.         bSuccess = FALSE;
  251.     }
  252.  
  253.     nRet = fmts.extension.Validate(); 
  254.     if(nRet) 
  255.     {
  256.         PrintError(NULL, NO_LINE, nRet);
  257.         bSuccess = FALSE;
  258.     }
  259.  
  260.     nRet = fmts.index.Validate(); 
  261.     if(nRet) 
  262.     {
  263.         PrintError(NULL, NO_LINE, nRet);
  264.         bSuccess = FALSE;
  265.     }
  266.  
  267.     nRet = fmts.token.Validate(); 
  268.     if(nRet) 
  269.     {
  270.         PrintError(NULL, NO_LINE, nRet);
  271.         bSuccess = FALSE;
  272.     }
  273.  
  274.     return bSuccess;
  275. }
  276.  
  277.